//
//  ZBLoginViewController.m
//  ZhiBo
//
//  Created by huangtie on 2017/11/5.
//  Copyright © 2017年 huangtie. All rights reserved.
//

#import "ZBLoginViewController.h"
#import "ZBLoginCell.h"
#import "ZBLoginLogoView.h"

#define LOGO_RECT_HEIGHT FLEXIBLE_WIDTH_55INCH(245)

@interface ZBLoginViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UITableView *tableView;

@property (nonatomic , strong) NSArray <ZBLoginCell *> *cells;

@property (nonatomic , strong) ZBLoginLogoView *logoView;

@property (nonatomic , strong) ZBLoginCell *accountCell;
@property (nonatomic , strong) ZBLoginCell *pwdCell;

@end

@implementation ZBLoginViewController

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    self.navigationItem.leftBarButtonItem = [SMHelper customBarItemWithTitle:@"取消" target:self action:@selector(leftItemClick:)];
    
    self.accountCell = [[ZBLoginCell alloc] init];
    self.accountCell.textField.placeholder = @"请输入账户";
    self.accountCell.textField.text = [SMUserManager loadTelString];
    
    self.pwdCell = [[ZBLoginCell alloc] init];
    self.pwdCell.textField.placeholder = @"请输入密码";
    [self.pwdCell.textField setSecureTextEntry:YES];
    self.cells = @[self.accountCell , self.pwdCell];
    
    self.logoView = [[ZBLoginLogoView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, LOGO_RECT_HEIGHT)];
    [self.view addSubview:self.logoView];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.height = self.view.height - self.logoView.height;
    self.tableView.top = self.logoView.bottom;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.tableFooterView = [self footerView];
    [self.view addSubview:self.tableView];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)leftItemClick:(id)sender
{
    if (self.cancelBlock) {
        self.cancelBlock();
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (UIView *)footerView
{
    UIView *footer = [[UIView alloc] init];
    footer.size = CGSizeMake(SCREEN_WIDTH, 65);
    footer.backgroundColor = [UIColor whiteColor];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.size = CGSizeMake(footer.width - 140, footer.height - 20);
    loginBtn.centerX = footer.width / 2;
    loginBtn.centerY = footer.height / 2;
    loginBtn.backgroundColor = UICOLOR_ARGB(0xfffb3e3f);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = FONT_NORMAL_14;
    loginBtn.layer.cornerRadius = loginBtn.height / 2;
    loginBtn.clipsToBounds = YES;
    [loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:loginBtn];
    return footer;
}

- (void)loginClick
{
    [self.view endEditing:YES];
    
    if(!self.accountCell.textField.text.length){
        [self.view makeToast:@"请输入账户名!"];
        return;
    }
    
    if(!self.pwdCell.textField.text.length){
        [self.view makeToast:@"请输入密码!"];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    WS(weakSelf)
    [SMNetwork requestOnPOST:SM_Url_Login param:@{@"account" : self.accountCell.textField.text , @"password" : self.pwdCell.textField.text} completion:^(NSURLSessionDataTask * _Nullable task, id  _Nullable result, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if (!error) {
            if ([[result objectForKey:@"code"] integerValue] == 200) {
                [SMUserManager saveTelString:self.accountCell.textField.text];
                
                NSDictionary *data = [result objectForKey:@"data"];
                NSString *token = [data objectForKey:@"token"];
                [SMUserManager shareManager].token = token;
                if (weakSelf.loginSuccessBlock) {
                    weakSelf.loginSuccessBlock(token);
                }
                [weakSelf dismissViewControllerAnimated:YES completion:nil];
            }else{
                [weakSelf.view makeToast:[result objectForKey:@"msg"]];
            }
        }else{
            [weakSelf.view makeToast:TIP_LOAD_FAILED];
        }
    }];
}

#pragma mark DELEGATE-TABLEVIEW
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cells.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cells[indexPath.row];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cells[indexPath.row].height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

#pragma mark DELEGATE-KEYBORD
- (void)keyboardWillShow:(NSNotification *)notification
{
    CGFloat moveOffsetY = 0;
    switch ((NSInteger)kScreenHeight) {
        case 736:
            moveOffsetY = 60;
            break;
        case 667:
            moveOffsetY = 70;
            break;
        case 568:
            moveOffsetY = 100;
            break;
        case 480:
            moveOffsetY = 130;
            break;
    }
    self.logoView.height = LOGO_RECT_HEIGHT - moveOffsetY;
    [UIView animateWithDuration:.3 animations:^{
        self.tableView.top = self.logoView.bottom;
    }];
}

- (void)keyboardWillHide:(NSNotification *)notif
{
    self.logoView.top = 0;
    self.logoView.height = LOGO_RECT_HEIGHT;
    [UIView animateWithDuration:.3 animations:^{
        self.tableView.top = self.logoView.bottom;
    }];
}




@end
